package com.zhht.htzx.adapter;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.content.Context;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;
import com.nsgf.library.data.bean.OrderListBean;
import com.nsgf.library.ui.adapter.SimpleDataBindingPositionAdapter;
import com.nsgf.library.ui.adapter.ViewHolder;
import com.zhht.htzx.R;
import com.zhht.htzx.databinding.ItemBusinessOrderBinding;
import com.zhht.htzx.databinding.ItemOrderBinding;
import com.zhht.htzx.databinding.ItemOrderBusinessProductBinding;
import com.zhht.htzx.databinding.ItemOrderProductBinding;

/**
 * 订单列表适配器
 */
public class OrderBusinessAdapter extends SimpleDataBindingPositionAdapter<OrderListBean.ListBean, ItemBusinessOrderBinding> {

    public OrderBusinessAdapter(Context context) {
        super(context, R.layout.item_business_order, new DiffUtil.ItemCallback<OrderListBean.ListBean>() {
            @Override
            public boolean areItemsTheSame(@NonNull OrderListBean.ListBean oldItem,
                                           @NonNull OrderListBean.ListBean newItem) {
                return oldItem.getId().equals(newItem.getId());
            }

            @Override
            public boolean areContentsTheSame(@NonNull OrderListBean.ListBean oldItem,
                                              @NonNull OrderListBean.ListBean newItem) {
                // 首先比较status
                if (oldItem.getStatus() != newItem.getStatus()) {
                    Log.d("OrderAdapter", "status不同: old=" + oldItem.getStatus() + ", new=" + newItem.getStatus());
                    return false;
                }

                // 比较items列表
                if (oldItem.getItems() == null && newItem.getItems() == null) {
                    return true;
                }
                if (oldItem.getItems() == null || newItem.getItems() == null) {
                    Log.d("OrderAdapter", "items一个为null");
                    return false;
                }
                if (oldItem.getItems().size() != newItem.getItems().size()) {
                    Log.d("OrderAdapter", "items大小不同: old=" + oldItem.getItems().size() + ", new=" + newItem.getItems().size());
                    return false;
                }

                // 比较每个item的内容
                for (int i = 0; i < oldItem.getItems().size(); i++) {
                    OrderListBean.ListBean.ItemsBean oldItemsBean = oldItem.getItems().get(i);
                    OrderListBean.ListBean.ItemsBean newItemsBean = newItem.getItems().get(i);

                    if (oldItemsBean.getId() != newItemsBean.getId() ||
                            oldItemsBean.getAfterSaleStatus() != newItemsBean.getAfterSaleStatus()) {
                        Log.d("OrderAdapter", "item内容不同: position=" + i
                                + ", oldId=" + oldItemsBean.getId() + ", newId=" + newItemsBean.getId()
                                + ", oldAfterSaleStatus=" + oldItemsBean.getAfterSaleStatus()
                                + ", newAfterSaleStatus=" + newItemsBean.getAfterSaleStatus());
                        return false;
                    }
                }

                return true;
            }
        });
    }


    private OnItemClickListener listener;

    public void setOnClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    protected void onBindItem(ItemBusinessOrderBinding binding, OrderListBean.ListBean item, ViewHolder holder, int position) {

        binding.setVm(item);

        binding.recyclerView.setNestedScrollingEnabled(false);
        SimpleDataBindingAdapter<OrderListBean.ListBean.ItemsBean,
                ItemOrderBusinessProductBinding> mAdapter;
        mAdapter =new SimpleDataBindingAdapter<OrderListBean.ListBean.ItemsBean,
                ItemOrderBusinessProductBinding>(mContext,
                R.layout.item_order_business_product, new DiffUtil.ItemCallback<OrderListBean.ListBean.ItemsBean>() {
            @Override
            public boolean areItemsTheSame(@NonNull OrderListBean.ListBean.ItemsBean oldItem, @NonNull OrderListBean.ListBean.ItemsBean newItem) {
                return false;
            }

            @Override
            public boolean areContentsTheSame(@NonNull OrderListBean.ListBean.ItemsBean oldItem, @NonNull OrderListBean.ListBean.ItemsBean newItem) {
                return false;
            }
        }) {
            @Override
            protected void onBindItem(ItemOrderBusinessProductBinding binding,
                                      OrderListBean.ListBean.ItemsBean childItem,
                                      RecyclerView.ViewHolder holder) {
                binding.setVm(childItem);

            }
        };
        binding.setAdapterItem(mAdapter);

        mAdapter.submitList(item.getItems());
    }

}